<script setup>

import {ref} from "vue";
import {ElMessage} from "element-plus";
import axios from "axios";
import {useRouter} from "vue-router";

const router = useRouter()
const form = ref({
  username: '',
  password: '',
  name: '',
  sex: '',
  phone: ''
})

const handleRegister = async () => {
  try {
    if (!form.value.username || !form.value.password || !form.value.name || !form.value.phone) {
      return ElMessage.warning('请填写完整信息')
    }

    const { data } = await axios.post('http://localhost:8080/user/register', {
      no: form.value.username,
      password: form.value.password,
      name: form.value.name,
      sex: form.value.sex,
      phone: form.value.phone
    })

    if (data.code === 200) {
      ElMessage.success('注册成功')
      await router.push('/login')
    } else {
      ElMessage.error(data.msg)
    }
  } catch (error) {
    ElMessage.error(error.response?.data?.msg || '注册失败')
  }
}
</script>
<template>
  <div class="register-wrapper">
    <div class="register-card">
      <div class="register-header">
        <h2>用户注册</h2>
        <p>请填写以下信息完成注册</p>
      </div>

      <el-form :model="form" class="register-form" label-width="80px">
        <el-form-item label="账号">
          <el-input
              v-model="form.username"
              placeholder="请输入您的账号"
              prefix-icon="el-icon-user"
          />
        </el-form-item>

        <el-form-item label="密码">
          <el-input
              v-model="form.password"
              type="password"
              placeholder="请输入您的密码"
              show-password
              prefix-icon="el-icon-lock"
          />
        </el-form-item>

        <el-form-item label="姓名">
          <el-input
              v-model="form.name"
              placeholder="请输入您的姓名"
              prefix-icon="el-icon-edit"
          />
        </el-form-item>
        <el-form-item label="性别">
          <el-select v-model="form.sex" placeholder="请选择您的性别" style="width: 300px">
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="手机号">
          <el-input
              v-model="form.phone"
              placeholder="请输入您的手机号"
              prefix-icon="el-icon-phone"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="register-btn" @click="handleRegister">
            注册
          </el-button>
        </el-form-item>
      </el-form>

      <div class="footer">
        <el-link type="primary" @click="router.push('/login')">已有账号？立即登录</el-link>
      </div>
    </div>
  </div>
</template>

<style scoped>
.register-wrapper {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #f5f7fa;
}

.register-card {
  width: 400px; /* 调整宽度 */
  padding: 30px;
  background: white;
  border-radius: 10px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  text-align: center;
}

.register-header h2 {
  color: #333;
  font-size: 22px;
  margin-bottom: 10px;
}

.register-header p {
  color: #666;
  font-size: 14px;
  margin: 0;
}

.register-form {
  margin-top: 20px;
  width: 100%; /* 确保表单宽度占满父容器 */
}

.register-form .el-form-item:last-child {
  padding: 0 -2px; /* 左右各 20px 的间距 */
}


.register-form .el-input {
  width: calc(100% - 20px); /* 减少输入框宽度，增加右边距 */
  margin-right: 20px; /* 增加右边距 */
}

.register-btn {
  width: 100%;
  height: 40px;
  background: linear-gradient(to right, #4facfe, #00f2fe);
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 16px;
  cursor: pointer;
  transition: background 0.3s ease;
  display: flex;
  justify-content: center; /* 让按钮文本居中 */
  align-items: center;
  margin-right: 80px;
}

.register-btn:hover {
  background: linear-gradient(to right, #00c6fb, #005bea);
}

.footer {
  margin-top: 20px;
  display: flex;
  justify-content: center;
  font-size: 13px;
}
</style>


